<?php
    require_ONCE("global.php");
    require_ONCE("HttpApi.php");
    require_ONCE("db.php");
    
    if (!isset($_SESSION['user']))
    {
        header("Location:login.php");
        exit();
    }
    
    $userid = $_SESSION['user']["id"];
    $namepaths = array();
    $paths = array();
    $groupid = NULL;
    $ownerid = $userid;
    
    if (isset($_GET["groupid"]))
    {
        $groupid = $_GET["groupid"];
        if ($groupid != NULL)
        {
            $ownerid = $groupid;
            $groupname = $db->fetch_first("select username from users where userid=$groupid;");
        }
    }

    if ($groupid != NULL && !checkgroup($userid, $groupid))
    {
        echo "you are not belonged to this group, user = $userid, group = $groupid";
        exit(0);
    }

    if (isset($_GET["dirid"]))
    {
        $dirid = $_GET["dirid"];
    }
    else
    {
        $dirid = 0;
    }
    
    if (isset($_GET["showdel"]))
    {
        $showdel = $_GET["showdel"];
    }
    else
    {
        $showdel = false;
    }
    if ($dirid == 0)
    {
        $path = "0";
        $namepath="";
    }
    else
    {
        $path = $_GET["path"];
        $currentpath = $path;
        $pos = strrpos($path, ".");
        if ($pos === false)
        {
            $upper = "0";
        }
        else
        {
            $upperpath = substr($path, 0, $pos);
            $upper = substr($path, $pos+1);
        }
        $path = $path.".".$dirid;
        
        //name path
        $namepath = $_GET["namepath"];
        $pos = strrpos($namepath, "/");
        $uppernamepath = substr($namepath, 0, $pos);
        
        $namepaths = split('[/]', $namepath);
        $paths = split('[.]', $path);
        
    }
    
    $url = 'http://'.$DS_HOST.'/'.$ownerid.'/'.$dirid.'/lsdir';
    //echo $url;
    $http = new HttpApi();
    $params = Array(
        //"name" => "abc",
    );
    
    
    $data = $http->makeRequest($url, $params);
    //print_r($data);
    $data = json_decode($data);
    $data = get_object_vars($data);
    $totalcount = 0;
    if (count($data['array']) > 0)
    {
        foreach($data['array'] as $row)
        {
            $row = get_object_vars($row);
            if ($showdel || !$row["isdeleted"])
            {
                $totalcount ++;
            }
            
            if ($row["type"] == "DIR")
            {
                savedirid($row["userid"], $row["dirid"], $row["name"]);
            }
        }
    }
    
    
    // event
    $sql = "select * from op_log where ownerid = $ownerid order by ctime desc limit 0,20";
    $logs = $db->fetchAll($sql);
    
    // groups
    $sql = "select u.username, u.userid from users as u join group_rel g on u.userid = g.groupid where g.userid = $userid;";
    $groups = $db->fetchAll($sql);
    
    include("templates/main.html");
?>

